{"componentChunkName":"component---src-templates-blog-template-js","path":"/blog/migrating-to-gatsby","result":{"data":{"markdownRemark":{"html":"<p>This blog used to be one long HTML file. This was fine to get started as there was no barrier in getting set up but was getting a bit unwieldy after a few years.</p>\n<p>I've been playing with Gatsby for a while so have converted the old site into Gatsby.</p>\n<p>I started with a simple <a href=\"https://github.com/gatsbyjs/gatsby-starter-default.git\" target=\"_blank\" rel=\"noopener noreferrer\">Gatsby starter site</a> to build out from. </p>\n<p>I wanted to use markdown to be able to add blog posts quite easily. For now I will edit MD files by hand with this sets me up nicely to use <a href=\"https://www.netlifycms.org/\" target=\"_blank\" rel=\"noopener noreferrer\">NetlifyCMS</a> in the future if I want to.</p>\n<p>The key gatsby plugin for this is <code>gatsby-transformer-remark</code>.</p>\n<p>Then to create empty markdown pages with the correct frontmatter (the properties surrounded by triple-dashes used to set up the page in the site) I started with <em>empty.md</em>, which was a simple file like this:</p>\n<pre><code>---\nslug: \"/blog/empty-sample\"\ndate: \"2099-01-02\"\ntitle: \"EMPTY?\"\ntemplate: \"blogTemplate\"\n---\n\nLoret ipsum\n</code></pre>\n<p>Then I wrote few bash functions to automate the creation of a new markdown file for each blog post that I was about to migrate from the old HTML page. The usage is</p>\n<pre><code>mknewmd \"Book review Pro ASP.NET Core MVC 2 by Adam Freeman\"\n</code></pre>\n<p>which will create a file named <code>book-review-pro-aspnet-core-mvc-2-by-adam-freeman.md</code> with the content:</p>\n<pre><code>---\nslug: \"/blog/book-review-pro-aspnet-core-mvc-2-by-adam-freeman\"\ndate: \"2099-01-02\"\ntitle: \"Book review: Pro ASP.NET Core MVC 2 by Adam Freeman\"\ntemplate: \"blogTemplate\"\n---\n\nLoret ipsum\n</code></pre>\n<p>ready for me to update the date and the markdown (more later).</p>\n<p>The <code>mknewmd</code> function relied on a few others.</p>\n<p>Firstly</p>\n<h3>lowerdashed</h3>\n<pre><code>lowerdashed() {  hyphensInsteadOfSpaces=${1// /-}  ; hyphensAndAlpaNumerics=$(sed  's/[^a-zA-Z0-9-]//g' &#x3C;&#x3C;&#x3C; \"$hyphensInsteadOfSpaces\" ) ; lowerAndHyphenated=$(sed  's/\\(.*\\)/\\L\\1/' &#x3C;&#x3C;&#x3C; \"$hyphensAndAlpaNumerics\" ) ; echo \"$lowerAndHyphenated\" ; }\n</code></pre>\n<p>takes the input var and </p>\n<ul>\n<li>replaces the spaces with hyphens</li>\n<li>strips out non-alphanumeric (and hyphen) characters </li>\n<li>echoes out the cleaned string (in this case <em>book-review-pro-aspnet-core-mvc-2-by-adam-freeman</em>).</li>\n</ul>\n<h3>mknewmd</h3>\n<pre><code>mknewmd() { cleaned=$(lowerdashed \"$1\") ;  cp src/markdown-pages/empty.md \"src/markdown-pages/$cleaned.md\" ; updateslug $cleaned ; updatetitle \"$1\" $cleaned ;  }\n</code></pre>\n<p>The var <code>cleaned</code> is set to the output of <code>lowerdashed</code>. This is used to copy <code>empty.md</code> into a new file with that name. Then the two <code>update</code> functions update the newly-created file.</p>\n<h3>updateslug</h3>\n<pre><code>updateslug() { sed -i \"s/empty-sample/$1/g\" \"src/markdown-pages/${1}.md\" ;  }                                                               \n</code></pre>\n<p>updates the slug from the <code>empty-sample</code> placeholder to the cleaned string (in this case <em>book-review-pro-aspnet-core-mvc-2-by-adam-freeman</em>) giving the slug used for the post URL.</p>\n<p>(If you want to find the definition of the function try <code>declare -f updateslug</code>)</p>\n<h3>updatetitle</h3>\n<pre><code>updatetitle() { sed -i \"s/EMPTY?/${1}/g\" \"src/markdown-pages/${2}.md\" ; }\n</code></pre>\n<p>updates the blog post title from the <code>EMPTY?</code> placeholder to the uncleaned string (in this case <em>Book review: Pro ASP.NET Core MVC 2 by Adam Freeman</em>).</p>\n<h3>Generating the markdown</h3>\n<p>The Html was converted to Markdown using <a href=\"https://convert-tool.com/conversion/html-to-markdown\" target=\"_blank\" rel=\"noopener noreferrer\">https://convert-tool.com/conversion/html-to-markdown</a> </p>","frontmatter":{"date":"October 11, 2020","slug":"/blog/migrating-to-gatsby","title":"Migrating to Gatsby"}}},"pageContext":{"slug":"/blog/migrating-to-gatsby"}},"staticQueryHashes":["3649515864"]}